//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES OR INDEMNITIES.
//

#ifndef __EONS_H__
#define __EONS_H__

#include <Toolbox.h>
#include <ril.h>

struct OPLStruct
{
    char szPLMN[16];
    DWORD dwLACa;
    DWORD dwLACz;
    DWORD dwIndex;
};

class EONS
{
public:
    EONS();
    EONS( const EONS& ref );
    virtual ~EONS();

    void Clear();

    virtual HRESULT Read();

    BOOL Ready() const;

    BOOL Search( DWORD dwPLMN, __opt RILOPERATORNAMES *pRON ) const;
    BOOL Search( DWORD dwPLMN, DWORD dwLAC, __opt RILOPERATORNAMES *pRON ) const;

    void Dump() const;

protected:

    static const DWORD EFopl;
    static const DWORD EFpnn;

    virtual HRESULT ReadData();

    virtual HRESULT ReadOPLData();
    virtual HRESULT ReadPNNData();

    BOOL DecodeNetworkNameTLV( const char *cptr, __in_ecount(dwOut) char *pszOUT, DWORD dwOut ) const;

    GrowableThingCollection<OPLStruct>        m_opl;
    GrowableThingCollection<RILOPERATORNAMES> m_pnn;

private:

    EONS& operator= ( const EONS& ref );

    static const char WILDCARD;
    static const char DONTCARE;

    static const int IEIfullname;
    static const int IEIshortname;

    static DWORD s_ThreadStartCallback( LPVOID lpThreadData );

    BOOL Match( DWORD dwPLMN, DWORD dwLAC, __opt RILOPERATORNAMES *pRON ) const;

    void Validate() const;

    mutable CRITICAL_SECTION m_cs;
    HANDLE m_hThread;
    BOOL m_fDoneReading;
};

/*************************/
/*************************/

class EONSCache : public EONS
{
public:
    EONSCache();
    EONSCache( const EONS& ref );
    virtual ~EONSCache();

    virtual HRESULT Read();
    virtual HRESULT Write();

    void Invalidate();

protected:
    static const TCHAR RegistryPath[];

    /* read from cache */
    virtual HRESULT ReadOPLData();
    virtual HRESULT ReadPNNData();

    /* write to cache */
    virtual HRESULT WriteOPLData();
    virtual HRESULT WritePNNData();

private:
    EONSCache( const EONSCache& ref );
    EONSCache& operator= ( const EONSCache& ref );
};

#endif
